/*
 License
 
 Copyright © 2012 Kevin Kimmich
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to 
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so, 
 subject to the following conditions:
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "kuato_types.h"

static int __match_address(method* m, http_match_rule_t* rule,int i)
{
	uint32_t addr = m->ctx->addr.sin_addr.s_addr & rule->expression.nmask[i];
	if(addr == rule->expression.addr[i])
		return 1;
	
	return 0;
}

static int __match_path(method* m, http_match_rule_t* rule, int i)
{
	if(regexec(&rule->expression.path_regex[i],m->req.uri.path,0,NULL,0)==0)
		return 1;

	return 0;
}

static int __match_method(method* m, http_match_rule_t* rule,int i)
{
	int rv = 0;
	//method is a comma separated list of GET,PUT,POST,DELETE,*
	char** fields;
	int count = split((char*)rule->expression.method[i],strlen(rule->expression.method[i]),&fields,",");
	int ii;
	for(ii=0;ii<count;ii++)
	{
		if((rv==0) && ((strcmp(fields[ii],"*")==0) || (strcmp(fields[ii],m->req.method)==0)))
		{
			rv = 1;
		}
		free(fields[ii]);
	}
	free(fields);
	
	return rv;
}

static int __match_context(method* m, http_match_rule_t* rule,int i)
{
	if(m->ctx->srvctx == rule->expression.ctx[i])
		return 1;
	return 0;
}

http_match_rule_t* get_match_rule( domain_object* d, method* m )
{
	list_node_t* node;
	for(node = d->match_rules.head.next;node;node=node->next)
	{
		http_match_rule_t* rule = (http_match_rule_t*)node;
		int comparisons[MAX_COMPARISONS];
		int valid_count = 0;
		int i;

		for(i=0;i<MAX_COMPARISONS;i++)
			comparisons[i]=-1;
		
		int test = 0;
		
		for(i=0;i<MAX_COMPARISONS;i++)
		{
			switch(rule->expression.type[i])
			{
				case match_any:
					comparisons[valid_count++]=1;
					break;
					
				case match_address:
					comparisons[valid_count++] = __match_address(m,rule,i);
					break;
					
				case match_method:
					comparisons[valid_count++] = __match_method(m,rule,i);
					break;
					
				case match_path:
					comparisons[valid_count++] = __match_path(m,rule,i);
					break;

				case match_context:
					comparisons[valid_count++] = __match_context(m,rule,i);
					break;
					
				case invert_match_address:
					comparisons[valid_count++] = !__match_address(m,rule,i);
					break;
					
				case invert_match_method:
					comparisons[valid_count++] = !__match_method(m,rule,i);
					break;
					
				case invert_match_path:
					comparisons[valid_count++] = !__match_path(m,rule,i);
					break;

				case invert_match_context:
					comparisons[valid_count++] = !__match_context(m,rule,i);
					break;
				
				case match_null:
					test |= (1<<i);
					break;
					
				default:
					assert(0);
			};
		}
		
		/*if(test!=7 && test!=6 && test != 4 && test!=0)
		{
			assert(0);//invalid match expression
		}*/

		int result = 0;
		int combination=0;
		for(i=0;i<valid_count;i++)
		{
			if(i==0)
				result = comparisons[i];
			else
			{
				if(rule->expression.combination[combination++]==match_AND)
					result = result && comparisons[i];
				else
					result = result || comparisons[i];
			}
		}
		
		if(result)
			return rule;
	}
	
	return NULL;
}
